layui.use(['form','layer','jquery','laypage','table'],function(){
    var layer=layui.layer,table=layui.table,form=layui.form,$=layui.jquery;

    //方法级渲染
    var tabopt={
        id: 'iotdispatch',
        elem: '#iotdispatchList'
        , height: 'full-200'
        ,width:'100%'
        ,loading: true
        ,cellMinWidth: '80'
        , url: '/iot/dispatch/list/'+$("#port").val()
        ,defaultToolbar: ['exports','filter','print']
        ,toolbar:'#toolbarDemo'
        , cols: [[
            {type:'checkbox', fixed: 'left'}
            , {field: 'port',title: '源端口',width:'160'}
            , {field: 'address', title: '消息分发ip',width:'160'}
            , {field: 'mqport', title: '分发ip端口',width:'160'}
            , {field: 'virtualHost',title:'所属虚拟主机',width:'160'}
            , {field: 'exchange',title:'交换器名称',width:'160'}
            , {field: 'exchangeType',title:'交换器类型',width:'160'}
            , {field: 'routingKey',title:'消息匹配规则',width:'160'}
            , {field: 'queueName',title:'消息分发队列',width:'160'}
            , {field: 'publisherConfirm',title:'消息发布确认',width:'160',templet:function (d) {
                    if (d.publisherConfirm =='1'){ return '是';}else {
                        return '否';
                    }
                }}
            , {field: 'username',title:'用户名',width:'160'}
            , {field: 'password',title:'密码',width:'160'}
            , {field: 'isOpen',title:'是否开启',width:'160',templet:function(d){
                   if (d.isOpen =='1'){ return '启用';}else {
                       return '禁用';
                   }
            }}
            ,{field: 'right', title: '操作',toolbar: "#barDemo",fixed:'right',width:'320'}
        ]]
        , page: true
        , request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusCode: 200 //成功的状态码，默认：0
           , countName: 'count' //数据总数的字段名称，默认：count
            , dataName: 'data' //数据列表的字段名称，默认：data
        }
        ,even: true
    };
    table.render(tabopt);

    /*
   * 监听行事件
   * */
    table.on('tool(iotdispatch)',function (obj) {
        var data=obj.data;
        tableoperation[obj.event](data.id);
    });

    //监听头事件
    table.on('toolbar(iotdispatch)', function(obj){
        console.log(obj);
        var checkStatus = table.checkStatus(obj.config.id);
        console.log(checkStatus);
        var data = checkStatus.data;
        console.log(data);
        switch(obj.event){
            case 'delbatch':
                if (data.length<1){
                    layer.msg("请选择一行！！", {shift: 6, icon: 2});
                    return;
                }
                var arr=[];
                data.forEach(function (item,index) {
                    arr.push(item.id);
                });
                console.log(arr);
                tableoperation[obj.event](arr.join(","));
                break;
        };
    });

    var tableoperation={
        delbatch:function (ids) {
            $.ajax({
                url: "/iot/dispatch/removeBatch/"+ids,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
        ,start:function (id) {
            $.ajax({
                url: "/iot/dispatch/start/"+id,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
        ,view :function (id) {
            layer.open({
                title : "详情",
                area: ['90%', '90%'],
                offset: 't',
                fix: false,
                maxmin: true,
                shadeClose: false,
                shade: 0.4,
                type : 2,
                content : "/iot/dispatch/view/"+id
                ,cancel:function (index,layero) {
                    /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                    location.reload();
                }
            })
        }
        ,cancel:function (id) {
            $.ajax({
                url: "/iot/dispatch/cancel/"+id,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
    };


})
